﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Mimoza.Transport
{
    public class Transport
    {
        const int DefaultTimeout = 3 * 60 * 1000; // 3 mins in milliseconds

        static Transport()
        {
            m_dispatcher = new Dispatcher();
            ErrorReply.RegisterInTransport();
        }

        /// <summary>
        /// Delegate for processing incoming requests
        /// </summary>
        public delegate void Handler(TransferObject request,out TransferObject reply);

        /// <summary>
        ///  Create transport listen location. Create listener on all addresses on local host.
        /// </summary>
        static public Listener CreateListener(int port)
        {
            return new Listener(port);
        }

        /// <summary>
        ///  Create transport connection to remote host.
        /// </summary>
        public static Connection CreateConnection(string remoteIpOrHostName, int port)
        {
            return new Connection(remoteIpOrHostName, port);
        }

        /// <summary>
        ///  Create udp connection. Empty remoteIpOrHostName is anyaddr that equal broadcast.
        /// </summary>
        public static Connection CreateUDPConnection(string remoteIpOrHostName, int port)
        {
            UdpConnection conn = new UdpConnection(remoteIpOrHostName, port);
            conn.Connect();
            return conn;
        }

        /// <summary>
        ///  Create udp broadcast connection. This type of connection can't recieve Replies.
        /// </summary>
        public static Connection CreateUDPBroadcast(int port)
        {
            return CreateUDPConnection("", port);
        }

        /// <summary>
        ///  Create serer udp connection. This connection can receive request from client and send replies.
        /// </summary>
        public static Connection CreateServerUDPConnection(int port)
        {
            UdpConnection conn = new UdpConnection(LocalInfo.HostName, port);
            conn.Bind();
            return conn;
        }

        /// <summary>
        /// Timeout for send and receive operations.
        /// </summary>
        public static int SendReceiveTimeout
        {
            get
            {
                return m_sendRecvTimeout;
            }
            set
            {
                m_sendRecvTimeout = value;
            }
        }

        /// <summary>
        /// Timeout for connect operation.
        /// </summary>
        public static int ConnectTimeout
        {
            get
            {
                return m_connectTimeout;
            }
            set
            {
                m_connectTimeout = value;
            }
        }

        /// <summary>
        ///  Returns transport Dispatcher.
        /// </summary>
        public static Dispatcher Dispatcher
        {
            get
            {
                return m_dispatcher;
            }
        }

        static Dispatcher m_dispatcher;
        static int m_sendRecvTimeout = DefaultTimeout;
        static int m_connectTimeout = DefaultTimeout;
    }
}
